<template>
  <div class="goods-size-content">
    <div v-if="isEdited">
      <span>
        {{goodsRow.weight}}kg ({{goodsRow.length}}×{{goodsRow.width}}×{{goodsRow.height}}cm)
      </span>
      <span v-if="!hideEditButton">
         <i class="el-icon-edit mgl10" style="cursor: pointer" @click="showEditDialog" v-if="canEdit"></i>
      </span>
    </div>
    <div v-else>
      <template v-if="goodsRow.isTB">
        -
      </template>
      <template v-else>
        <el-button type="text" @click="copySameSize" v-if="canCopy" :loading="loading">{{ $t('stock.复用同Listing') }}</el-button>
        <el-button type="text" @click="showEditDialog" v-else :loading="loading">{{ $t('stock.编辑重量与尺寸') }}</el-button>
        <i class="el-icon-edit mgl10" style="cursor: pointer" @click="showEditDialog"></i>
      </template>
    </div>


    <el-dialog
      :visible.sync="dialog"
      width="600px"
      class="goods-size-dialog"
      :title="$t('stock.填写重量与尺寸')"
    >
      <div class="goods-size-dialog-content">
        <div class="goods-size-dialog-sub-title">
          SKU: {{goodsRow.sku}}
        </div>
        <div class="goods-size-dialog-form">
          <el-form
            ref="sizeForm"
            :model="currentForm"
            label-width="50px"
            label-position="left"
            :rules="rules"
          >
            <el-form-item
              :label="$t('table.重量')"
              prop="weight"
            >
              <el-input-number
                v-model="currentForm.weight"
                :controls="false"
                :min="0"
                :max="max"
                :precision="2"
                class="weight-input"
              >
              </el-input-number>
              <span class="mgl10">kg</span>

            </el-form-item>
            <el-form-item
              :label="$t('table.尺寸')"
              required
            >
              <div class="size-input-box">
                <el-form-item prop="length">
                  <el-input-number
                    v-model="currentForm.length"
                    class="size-input"
                    :min="0"
                    :max="max"
                    :precision="0"
                    :controls="false"
                  >
                  </el-input-number>
                  <span class="mgl10">{{ $t('stock.长(cm)') }}</span>
                </el-form-item>

                <el-form-item prop="width">
                  <el-input-number
                    v-model="currentForm.width"
                    class="size-input"
                    :min="0"
                    :max="max"
                    :precision="0"
                    :controls="false"
                  >
                  </el-input-number>
                  <span class="mgl10">{{ $t('stock.宽(cm)') }}</span>
                </el-form-item>

                <el-form-item prop="height">
                  <el-input-number
                    v-model="currentForm.height"
                    class="size-input"
                    :min="0"
                    :max="max"
                    :precision="0"
                    :controls="false"
                  >
                  </el-input-number>
                  <span class="mgl10">{{ $t('stock.高(cm)') }}</span>
                </el-form-item>

              </div>

            </el-form-item>
            <el-form-item>
              <el-button size="small" type="primary" @click="submitGoodsSize()" :loading="loading">{{$t('common.save')}}</el-button>
              <el-button size="small" @click="dialog = false">{{$t('common.cancel')}}</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {saveGoodsSize} from "@/api/goods";

export default {
  name: "goodsSize",
  mixins: [],
  props:{
    goodsRow: {
      type: Object,
      required: true,
    },
    goodsList: {
      type: Array,
      required: true
    },
    hideEditButton: {
      type: Boolean,
      default: false
    }
  },
  computed:{
    // 判断是否已经编辑过
    isEdited(){
      return !!this.goodsRow.weight
    },
    // 判断是否能编辑
    canEdit(){
      return this.goodsRow.isLockSize !== 1
    },
    // 判断是否能复制
    canCopy(){
      return this.goodsList.some(item=>{
        return !!item.weight;
      })
    }
  },
  data(){
    return {
      max: 999999, // 设置一个最大值，避免用户乱填 导致接口异常
      currentForm: {
        id: 0,
        weight: 0,
        length: 0,
        width: 0,
        height: 0,
        isSystemLabel: false,
      },
      rules: {
        weight: [
          {type:'number', required: true, message: this.$t('stock.重量请填写大于0的数字'), trigger: 'blur',min:0.1},
        ],
        length: [
          {type:'number', required: true, message: this.$t('stock.长请填写大于0的数字'), trigger: 'blur',min:1}
        ],
        width: [
          {type:'number', required: true, message: this.$t('stock.宽请填写大于0的数字'), trigger: 'blur',min:1},
        ],
        height: [
          {type:'number', required: true, message: this.$t('stock.高请填写大于0的数字'), trigger: 'blur',min:1},
        ],
      },
      dialog: false,
      loading: false,
    }
  },
  methods: {
    showEditDialog(){
      this.currentForm.id = this.goodsRow.id
      this.currentForm.weight = this.goodsRow.weight
      this.currentForm.length = this.goodsRow.length
      this.currentForm.width = this.goodsRow.width
      this.currentForm.height = this.goodsRow.height
      this.currentForm.isSystemLabel = this.goodsRow.isSystemLabel || this.goodsRow.isSystem
      if(this.currentForm.isSystemLabel){
        this.currentForm.id = this.goodsRow.systemLabelId
      }
      this.dialog = true
    },
    // 复制相同尺寸
    copySameSize(){
      // 找到对应有编辑过的listing
      const goodsCopy = this.goodsList.find(item=>{
        if(item.weight){
          return item
        }
      })
      if(goodsCopy){
        this.currentForm.id = this.goodsRow.id
        this.currentForm.weight = goodsCopy.weight
        this.currentForm.length = goodsCopy.length
        this.currentForm.width = goodsCopy.width
        this.currentForm.height = goodsCopy.height
        this.currentForm.isSystemLabel = this.goodsRow.isSystemLabel || this.goodsRow.isSystem
        if(this.currentForm.isSystemLabel){
          this.currentForm.id = this.goodsRow.systemLabelId
        }
        this.saveGoodsSize()
      }
    },
    // 提交表单数据
    submitGoodsSize(){
      this.$refs.sizeForm.validate(valid => {
        if(valid){
          this.saveGoodsSize()
        }
      })
    },
    saveGoodsSize(){
      this.loading = true
      saveGoodsSize(this.currentForm)
        .then(res=>{
          this.loading = false
          this.dialog = false
          this.goodsRow.weight = this.currentForm.weight
          this.goodsRow.length = this.currentForm.length
          this.goodsRow.width = this.currentForm.width
          this.goodsRow.height = this.currentForm.height
          this.$successMessage(res.message)
          this.$emit('saveGoodsSize')
        })
    },
  }
}

</script>

<style scoped lang="scss">
.goods-size-dialog{
  text-align: left;
  .goods-size-dialog-content{
    margin-top: -25px;
    .goods-size-dialog-sub-title{
      font-size: 14px;
      margin-bottom: 15px;
    }

    .goods-size-dialog-form{
      .weight-input{
        width: 100px;
      }
      .size-input-box{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        .size-input{
          width: 100px;
          ::v-deep .el-input-group__append{
            padding: 0 15px !important;
          }
        }
      }

    }
  }
}
</style>
